/**
**************************************************************************************
*Founder Effect                                                                      *
*Copyright (C) 2006-2007, 5d Studios (www.5d-Studios.com)                            *
*                                                                                    *
*This library is free software; you can redistribute it and/or                       *
*modify it under the terms of the GNU Lesser General Public                          *
*License as published by the Free Software Foundation; either                        *
*version 2.1 of the License, or (at your option) any later version.                  *
*                                                                                    *
*This library is distributed in the hope that it will be useful,                     *
*but WITHOUT ANY WARRANTY; without even the implied warranty of                      *
*MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU                   *
*Lesser General Public License for more details.                                     *
*                                                                                    *
*You should have received a copy of the GNU Lesser General Public                    *
*License along with this library; if not, write to the Free Software                 *
*Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA      *
**************************************************************************************
*/
#include "StdAfx.h"
#include "EntityView.h"

#include "../game/IEntity.h"
#include "../Kernel.h"

extern Kernel* g_kernel;

EntityView::EntityView(IEntity* ent)
{
    m_owner = ent;
    if ( m_owner )
    {
        if ( m_owner->m_attributes.Has( "dialog" ) )
        {
            m_speakerIcon = g_kernel->GetResource()->LoadSingleImage( "misc/speakerIcon.tga" );
        }
    }
}


/**
======================
Update the model to represent the correct 
Face of the entity
======================
*/
void EntityView::Update(long deltaTime)
{
    m_model.SetCurrentDirection( m_owner->GetFace() );
    m_model.SetCurrentState( m_owner->GetState() );
    
    // update the model
    m_model.Update(deltaTime); 
}

/**
======================
Render this view
======================
*/
void EntityView::Render( IRender *r, ICamera* cam )
{
    Vector2f position = m_owner->m_physObject.GetPosition();
    Vector2f offsetPos( position - cam->GetPosition() );
    offsetPos =  offsetPos + Vector2f( cam->GetViewPort().x, cam->GetViewPort().y );

    DrawModel(r, cam, offsetPos, &m_model );
    if ( m_owner->GetState() == ENT_STATES[TALKING] )
        DrawSpeakerIcon(r, cam, m_speakerIcon, m_owner, offsetPos );
    
    // show useful stats
    if ( g_kernel->GetCvars()->GetFloatValue("debug") ) 
        DrawDebugInfo(r, cam, m_owner, offsetPos );
    
}

/** Draw Character */
void EntityView::DrawModel(IRender* r, ICamera* cam, const Vector2f &offsetPos, Model* model )
{
    Image* image = &model->GetImage();
    /**
     * Cast a shadow
     */
    if ( m_owner->GetType() != TYPE_OBJECT ) 
    {
        int direction = m_owner->GetFace();
        int yOffset = m_owner->GetBounds().height;
        switch ( direction )
        {
        case NORTH: yOffset  /= 2; break;
        //case SOUTH: image->iFlags |= IF_MIRROR;
        //    break;
        default: image->iFlags |= IF_FLIP;
            break;
        }    
        r->SetColorEffect( 0, 0, 0, 0.289f );
        r->DrawSubImage(    image,                        // image of character
                            offsetPos.x,                  // x position
                            offsetPos.y + yOffset,        // y position
                            model->GetRow(),              // Row of animation
                            model->GetCurrentFrame()      // current frame
                       );
        image->iFlags = 0; // clear the flags
    }

    // Draw the entity
    r->SetColorEffect( 1, 1, 1 );   
    r->DrawSubImage(    image,                        // image of character
                        offsetPos.x,                  // x position
                        offsetPos.y,                  // y position
                        model->GetRow(),              // Row of animation
                        model->GetCurrentFrame()      // current frame
                   );
}

/** Draw Speaker Icon */
void EntityView::DrawSpeakerIcon( IRender* r, ICamera* cam, Image &speakerIcon, IEntity* ent, const Vector2f &offsetPos  )
{
    Rect bounds = ent->GetBounds();
    r->DrawImage( &speakerIcon, offsetPos.x + bounds.width / 2 + 20, offsetPos.y - bounds.height );
}
/** Draw Debug Info */
void EntityView::DrawDebugInfo (IRender* r, ICamera* cam, IEntity* ent, const Vector2f &offsetPos )
{
    Vector2f position = ent->m_physObject.GetPosition();
    Rect bounds = ent->GetBounds();

    r->SetColor(1, 1, 1 );
    r->DrawString(  offsetPos.x, offsetPos.y-30, ent->GetName().c_str() );
    r->DrawString(  offsetPos.x, offsetPos.y-10, "%6.2f,%6.2f", position.x, position.y );

    r->DrawString( offsetPos.x, offsetPos.y+80, "HP: %4d", ent->m_health );
    r->DrawString( offsetPos.x, offsetPos.y+100, ("State: "+ent->GetState()).c_str() );
    //r->DrawLine( offsetPos + (ent->m_vForward*20) , offsetPos );
    //r->DrawRect( offsetPos.x, offsetPos.y + bounds.height, bounds.width, bounds.height );

    //Rect rect = ent->GetReachRectangle();
    //r->DrawRect( rect.x, rect.y, rect.width, rect.height );
}


/**
========================
Apply an effect
========================
*/
void EntityView::Effect(int Flags)
{
    // TODO
}


EntityView::~EntityView(void)
{
}
